home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / role / larn.lha / tok.c < prev    next >
C/C++ Source or Header  |  1995-11-19  |  14KB  |  536 lines

  1. /* tok.c */
  2. /*
  3.    yylex()
  4.    flushall()
  5.    sethard()
  6.    readopts()
  7.    actual_readopts()
  8. */
  9. #ifdef VMS
  10. #include <types.h>
  11. #include <file.h>
  12. #include <iodef.h>
  13. #else VMS
  14. #include <sys/types.h>
  15. #ifdef SYSV
  16. #include <fcntl.h>
  17. # ifndef MSDOS
  18. #   include <termio.h>
  19. # endif
  20. #else SYSV
  21. # ifndef AMIGA
  22. #  ifndef MSDOS
  23. #   include <sys/ioctl.h>
  24. #  endif MSDOS
  25. # endif AMIGA
  26. #endif SYSV
  27. #endif VMS
  28.  
  29. #include <ctype.h>
  30. #include "header.h"
  31. #include "larndefs.h"
  32. #include "monsters.h"
  33. #include "objects.h"
  34. #include "player.h"
  35.  
  36. #ifdef __STDC__
  37. static void actual_readopts( char*, char* );
  38. #else
  39. static void actual_readopts();
  40. #endif
  41.  
  42. # define CHKPTINT   400
  43.  
  44. static char lastok=0;
  45. int yrepcount=0;
  46. char move_no_pickup = FALSE ;
  47.  
  48. #ifdef TIMECHECK
  49. int dayplay=0;
  50. #endif TIMECHECK
  51.  
  52. #ifndef FLUSHNO
  53. #define FLUSHNO 5
  54. #endif FLUSHNO
  55. static int flushno=FLUSHNO; /* input queue flushing threshold */
  56. #define MAXUM 52    /* maximum number of user re-named monsters */
  57. #define MAXMNAME 40 /* max length of a monster re-name */
  58. static char usermonster[MAXUM][MAXMNAME]; /* the user named monster name goes here */
  59. static char usermpoint=0;           /* the user monster pointer */
  60. #ifdef MSDOS
  61.  extern int rawio;
  62. #endif
  63.  
  64. /*
  65.     lexical analyzer for larn
  66.  */
  67. yylex()
  68.     {
  69.     char cc;
  70.     int ic;
  71.     char firsttime = TRUE;
  72.  
  73.     if (hit2flag)
  74.         {
  75.         hit2flag=0;
  76.         yrepcount=0;
  77.         return(' ');
  78.         }
  79.     if (yrepcount>0)
  80.         {
  81.         --yrepcount;
  82.         return(lastok);
  83.         }
  84.     else
  85.         yrepcount=0;
  86.     if (yrepcount==0) 
  87.         { 
  88.         bottomdo(); 
  89.         showplayer();               /* show where the player is */
  90.         move_no_pickup = FALSE;     /* clear 'm' flag */
  91.         }
  92.  
  93.     lflush();
  94.     while (1)
  95.         {
  96.         c[BYTESIN]++;
  97.         if (ckpflag)
  98.           if ((c[BYTESIN] % CHKPTINT) == 0) /* check for periodic checkpointing */
  99.             {
  100. #ifndef DOCHECKPOINTS
  101. #ifdef MSDOS|AMIGA
  102.             cursors();
  103.             lprcat("\nCheckpointing . . .");
  104.             savegame(ckpfile);
  105.             lprcat("\nDone\n");
  106.             showplayer();
  107.             lflush();
  108.             lflushall(); /* Kill any stored key strokes */
  109. #else
  110. #ifdef VMS
  111.             savegame(ckpfile);
  112. #else
  113.             wait(0);    /* wait for other forks to finish */
  114.             if (fork() == 0) { savegame(ckpfile); exit(); }
  115. #endif
  116. #endif
  117. #else
  118. #ifdef VMS
  119.             savegame(ckpfile);
  120. #else
  121.             wait(0);    /* wait for other forks to finish */
  122.             if (fork() == 0) { savegame(ckpfile); exit(); }
  123. #endif
  124. #endif
  125.  
  126. #ifdef TIMECHECK
  127.             if (dayplay==0)
  128.               if (playable())
  129.                 {
  130.                 cursor(1,19);
  131.                 lprcat("\nSorry, but it is now time for work.  Your game has been saved.\n"); beep();
  132.                 lflush();
  133.                 savegame(savefilename);
  134.                 wizard=nomove=1;
  135.                 sleep(4);
  136.                 died(-257);
  137.                 }
  138. #endif TIMECHECK
  139.  
  140.             }
  141. # ifdef AMIGA
  142.     FlushKBD();
  143. # else
  144. # ifndef MSDOS
  145. # ifdef VMS
  146.         /* If keyboard input buffer is too big then flush some? RDE */
  147.         /* Check this! but for now just ignore it... */
  148. # else
  149. #  ifndef SYSV
  150.         do /* if keyboard input buffer is too big, flush some of it */
  151.             {
  152.             ioctl(0,FIONREAD,&ic);
  153.             if (ic>flushno)   read(0,&cc,1);
  154.             }
  155.         while (ic>flushno);
  156. #  endif SYSV
  157. # endif VMS
  158. # endif MSDOS
  159. # endif AMIGA
  160.  
  161. # ifdef AMIGA
  162.     cc = AmGetch();
  163. # else
  164. # ifdef MSDOS
  165.         cc = ttgetch();
  166. # else MSDOS
  167. # ifdef VMS
  168.         cc = ttgetch();
  169. # else
  170.         if (read(0,&cc,1) != 1)
  171.             return(lastok = -1);
  172. # endif VMS
  173. # endif MSDOS
  174. # endif AMIGA
  175.         if (cc == '!')      /* ! shell escape */
  176.             {
  177.             resetscroll();  /* scrolling region, home, clear, no attributes */
  178.             clear();
  179. # ifdef AMIGA
  180.         DoShell();
  181. # else
  182. # ifdef MSDOS
  183.             doshell();
  184. # else MSDOS
  185. # ifdef VMS
  186.             lflush();
  187.             sncbr();
  188.             oneliner("");
  189.             scbr();
  190. # else VMS
  191.             lflush();
  192.             sncbr();
  193.             if ((ic=fork())==0) /* child */
  194.                 {
  195. #ifdef SYSV
  196.                 char *s, *getenv();
  197.                 if ((s=getenv("SHELL")) == (char *) 0)
  198.                     s = "/bin/sh";
  199.                 execl(s,"larn-shell", (char *)0);
  200.                 exit();
  201. #else
  202.                 execl("/bin/csh",0);
  203.                 exit();
  204.                 wait(0);
  205. #endif
  206.                 }
  207.             if (ic<0) /* error */
  208.                 {
  209.                 write(2,"Can't fork off a shell!\n",25);
  210.                 sleep(2);
  211.                 }
  212. #ifdef SYSV
  213.             else
  214.                 wait( (int *)0 );
  215. #endif SYSV
  216. # endif VMS
  217. # endif MSDOS
  218. # endif AMIGA
  219.             setscroll();
  220.             return(lastok = 'L'-64);    /* redisplay screen */
  221.             }
  222.  
  223.         /* get repeat count, showing to player
  224.         */
  225.         if ((cc <= '9') && (cc >= '0'))
  226.             {
  227.             yrepcount = yrepcount*10 + cc - '0';
  228.  
  229.             /* show count to player for feedback
  230.             */
  231.             if ( yrepcount >= 10 )
  232.                 {
  233.                 cursors();
  234.                 if (firsttime)
  235.                     lprcat("\n");
  236.                 lprintf("count: %d", (long)yrepcount );
  237.                 firsttime=FALSE;
  238.                 lflush();  /* show count */
  239.                 }
  240.             }
  241.         else
  242.             {
  243.             /* check for multi-character commands in command mode, and handle.
  244.             */
  245.             if ( cc == 'm' && !prompt_mode )
  246.                 {
  247.                 move_no_pickup = TRUE ;
  248.                 cc = ttgetch();
  249.                 }
  250.             if ( yrepcount > 0 )
  251.                 --yrepcount;
  252.             return(lastok = cc);
  253.             }
  254.         }
  255.     }
  256.  
  257. /*
  258.  *  flushall()  Function to flush all type-ahead in the input buffer
  259.  */
  260. lflushall()
  261.     {
  262. # ifdef AMIGA
  263.       FlushKBD();
  264. # else AMIGA
  265. # ifdef MSDOS
  266.     while (kbhit())
  267.         getch();
  268. # else MSDOS
  269. # ifdef VMS
  270.     /* Flush all type-ahead -- RDE */
  271.     extern int  iochan;     /* defined in IO.C  */
  272.     int     c;
  273.  
  274.     SYS$QIOW(0,iochan,IO$_READLBLK|IO$M_TIMED|IO$M_PURGE,0,0,0,&c,1,0,0,0,0);
  275.  
  276. # else VMS
  277. #ifdef SYSV
  278.     ioctl(0,TCFLSH,0);
  279. #else
  280.     char cc;
  281.     int ic;
  282.     for (;;) {      /* if keyboard input buffer is too big, flush some of it */
  283.         ioctl(0,FIONREAD,&ic);
  284.         if (ic<=0)
  285.             return;
  286.         while (ic>0) {
  287.             read(0,&cc,1);
  288.             --ic;
  289.         } /* gobble up the byte */
  290.     }
  291. # endif SYSV
  292. # endif VMS
  293. # endif MSDOS
  294. # endif AMIGA
  295. }
  296.  
  297. /*
  298.     function to set the desired hardness
  299.     enter with hard= -1 for default hardness, else any desired hardness
  300.  */
  301. sethard(hard)
  302. int hard;
  303. {
  304.     register int    j,k;
  305.     long        i;
  306.     struct monst    *mp;
  307.  
  308.     j=c[HARDGAME]; hashewon();
  309.     if (restorflag==0)  /* don't set c[HARDGAME] if restoring game */
  310.         {
  311.         if (hard >= 0) c[HARDGAME]= hard;
  312.         }
  313.     else c[HARDGAME]=j; /* set c[HARDGAME] to proper value if restoring game */
  314.  
  315.     if (k=c[HARDGAME])
  316.       for (j=0; j<=MAXMONST+8; j++) {
  317.         mp = &monster[j];
  318.         i = ((6+k) * mp->hitpoints + 1)/6;
  319.         mp->hitpoints = (i<0) ? 32767 : i;
  320.         i = ((6+k) * mp->damage + 1) / 5;
  321.         mp->damage = (i>127) ? 127 : i;
  322.         i = (10 * mp->gold)/(10+k);
  323.         mp->gold = (i>32767) ? 32767 : i;
  324.         i = mp->armorclass - k;
  325.         mp->armorclass = (i< -127) ? -127 : i;
  326.         i = (7*mp->experience)/(7+k) + 1;
  327.         mp->experience = (i<=0) ? 1 : i;
  328.     }
  329. }
  330.  
  331.  
  332. /*
  333.     function to read and process the larn options file
  334. */
  335. readopts()
  336.     {
  337.     register int j;
  338.     char original_objects = FALSE ;
  339.     char namenotchanged = TRUE;     /* set to 0 if a name is specified */
  340.  
  341. # ifdef MSDOS
  342.     if (plopen(optsfile) < 0)
  343. # else
  344.     if (lopen(optsfile) < 0)
  345. # endif
  346.         {
  347.         lprintf("Can't open options file \"%s\"\n", optsfile);
  348.         lflush();
  349.         sleep(1);
  350.         }
  351.     else
  352.         actual_readopts(&namenotchanged, &original_objects);
  353.  
  354.     if (namenotchanged)
  355.         strcpy(logname,loginname);
  356.  
  357.     /* original objects require object highlighting to be ON (in order
  358.        to distinguish between objects and monsters).  set up object list
  359.        properly.
  360.     */
  361.     if (original_objects)
  362.         {
  363.         boldobjects = TRUE ;
  364.         strncpy( objnamelist, original_objnamelist, MAXOBJECT );
  365.         }
  366.     else
  367.         strncpy( objnamelist, hacklike_objnamelist, MAXOBJECT );
  368.     objnamelist[MAXOBJECT] = '\0' ;
  369.  
  370.     /* now set all the invisible objects and monsters to have the
  371.        same appearance as the floor (as defined by the user)
  372.     */
  373.     objnamelist[OWALL] = wallc;
  374.  
  375.     objnamelist[0]        =
  376.     objnamelist[OIVTELETRAP]  =
  377.     objnamelist[OTRAPARROWIV] =
  378.     objnamelist[OIVDARTRAP]   =
  379.     objnamelist[OIVTRAPDOOR]  = floorc;
  380.     monstnamelist[0] =
  381.     monstnamelist[INVISIBLESTALKER] = floorc;
  382.     for (j=DEMONLORD; j<=DEMONPRINCE; j++)
  383.         monstnamelist[j] = floorc;
  384.     }
  385.  
  386. static void actual_readopts( namenotchanged, original_objects )
  387. char *namenotchanged;
  388. char *original_objects;
  389.     {
  390.     register char *i;
  391.     register int j,k;
  392.  
  393.     i = " ";
  394.     while (*i)
  395.         {
  396.         /* check for EOF
  397.         */
  398.         if ((i=(char *)lgetw()) == 0)
  399.             return;
  400. #if 0
  401.         while (*i && ((*i==' ') || (*i=='\t'))) i++; /* eat leading whitespace */
  402. #endif
  403.         /* leading # a comment, eat the rest of the line.  Handle multiple
  404.            comment lines in a row.
  405.         */
  406.         while (*i == '#')
  407.             {
  408.             char cc;
  409.             do
  410.                 cc = (char)lgetc();
  411.             while ( ( cc != '\n' ) && ( cc != NULL));
  412.             if ((i = (char *)lgetw()) == 0)
  413.                 return;
  414.             }
  415.  
  416.         if (strcmp(i,"bold-objects") == 0)
  417.             boldon=1;
  418.         else if (strcmp(i,"enable-checkpointing") == 0)
  419.             ckpflag=1;
  420.         else if (strcmp(i,"inverse-objects") == 0)
  421.             boldon=0;
  422.         else if (strcmp(i,"prompt-on-objects") == 0 )
  423.             prompt_mode = TRUE ;
  424.         else if (strcmp(i,"auto-pickup") == 0 )
  425.             auto_pickup = TRUE ;
  426.         else if (strcmp(i,"highlight-objects") == 0 )
  427.             boldobjects = TRUE ;
  428.         else if (strcmp(i,"original-objects") == 0 )
  429.             *original_objects = TRUE ;
  430.         else if (strcmp(i,"female") == 0)
  431.             sex=0; /* male or female */
  432. # ifdef MSDOS
  433.         else if (strcmp(i, "graphics:") == 0)
  434.             {
  435.             wallc = atoi(lgetw());
  436.             floorc = atoi(lgetw());
  437.             }
  438.         else if (strcmp(i, "larndir:") == 0)
  439.             {
  440.             if ((i=lgetw())==0)
  441.                 break;
  442.             strncpy(larndir, i, DIRLEN);
  443.             larndir[DIRLEN - 1] = 0;
  444.             }
  445.         else if (strcmp(i, "rawio") == 0)
  446.             rawio = 1;
  447.         else if (strcmp(i, "swapfile:") == 0)
  448.             {
  449.             if ((i = (char *)lgetw()) == 0)
  450.                 break;
  451.             strncpy(swapfile, i, PATHLEN);
  452.             swapfile[PATHLEN - 1] = 0;
  453.             }
  454.         else if (strcmp(i, "ramlevels:") == 0)
  455.             ramlevels = atoi(lgetw());
  456.         else if (strcmp(i, "cursor:") == 0)
  457.             {
  458.             cursorset = 1;
  459.             cursorstart = (unsigned char) atoi(lgetw());
  460.             cursorend = (unsigned char) atoi(lgetw());
  461.             }
  462.         else if (strcmp(i, "keypad") == 0)
  463.             keypad = 1;
  464.         else if (strcmp(i, "DECRainbow") == 0)
  465.             DECRainbow = 1;
  466. # endif
  467.         else if (strcmp(i,"monster:")== 0)   /* name favorite monster */
  468.             {
  469.             if ((i=(char *)lgetw())==0)
  470.                 break;
  471.             if (strlen(i)>=MAXMNAME)
  472.                 i[MAXMNAME-1]=0;
  473.             strcpy(usermonster[usermpoint],i);
  474.             if (usermpoint >= MAXUM)
  475.                 break; /* defined all of em */
  476.             if (isalpha(j=usermonster[usermpoint][0]))
  477.                 {
  478.                 for (k=1; k<MAXMONST+8; k++) /* find monster */
  479.                   if (monstnamelist[k] == j)
  480.                     {
  481.                     monster[k].name = &usermonster[usermpoint++][0];
  482.                     break;
  483.                     }
  484.                 }
  485.             }
  486.         else if (strcmp(i,"male") == 0)
  487.             sex=1;
  488.         else if (strcmp(i,"name:") == 0) /* defining players name */
  489.             {
  490.             if ((i=lgetw())==0)
  491.                 break;
  492.             if (strlen(i)>=LOGNAMESIZE)
  493.                 i[LOGNAMESIZE-1]=0;
  494.             strcpy(logname,i);
  495.             *namenotchanged = FALSE;
  496. # ifdef MSDOS
  497.             strcpy(loginname,i);
  498. # endif
  499.             }
  500.         else if (strcmp(i,"no-introduction") == 0)
  501.             nowelcome=1;
  502.         else if (strcmp(i,"no-beep") == 0)
  503.             nobeep=1;
  504. # ifndef MSDOS
  505.         else if (strcmp(i,"process-name:")== 0)
  506.             {
  507.             if ((i=lgetw())==0)
  508.                 break;
  509.             if (strlen(i)>=PSNAMESIZE)
  510.                 i[PSNAMESIZE-1]=0;
  511.             strcpy(psname,i);
  512.             }
  513.         else if (strcmp(i,"play-day-play") == 0)
  514. # ifdef TIMECHECK
  515.             dayplay=1;
  516. # else
  517.         ;
  518. # endif
  519. # endif
  520.         else if (strcmp(i,"savefile:") == 0) /* defining savefilename */
  521.             {
  522.             if ((i=lgetw())==0)
  523.                 break;
  524.             if (strlen(i)>=SAVEFILENAMESIZE) /* avoid overflow */
  525.                 i[SAVEFILENAMESIZE-1]=0;
  526.             strcpy(savefilename,i);
  527.             }
  528.         else
  529.             {
  530.             lprintf("Unknown option \"%s\"\n", i);
  531.             lflush();
  532.             sleep(1);
  533.             }
  534.         }
  535.     }
  536.